#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>


void test_1()//程序替换
{
  printf("begin...\n");
  printf("begin...\n");
  printf("begin...\n");
  printf("begin...\n");
  printf("我是一个进程，我的PID:%d\n",getpid());

  execl("/bin/ls","ls","-a","-l",NULL);

  printf("end...\n");
  printf("end...\n");
  printf("end...\n");
}


void test_2()//判断程序替换返回值
//当程序替换成功则不会有返回值，如果程序替换失败则一定有返回值
{
  pid_t id=fork();
  if(id==0)
  {
    //进入子进程
    printf("我是子进程，pid：%d\n",getpid());
    //不需要对execl函数进行返回值判断，只要子进程向后执行，则一定替换失败
    execl("/bin/ls","ls","-a","-l",NULL);
    exit(-1);
  
  }
  sleep(3);
  int status=0;
  printf("我是父进程 pid：%d\n",getpid());
  waitpid(id,&status,0);
  printf("子进程退出码：%d\n",WEXITSTATUS(status));
}
int main()
{
  //test_1();
  test_2();



  return 0;
}
